package com.atguigu.gulimall.auth.interceptor;

import cn.hutool.core.lang.UUID;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.constant.CartConstant;
import com.atguigu.common.vo.MemberRespVO;
import com.atguigu.gulimall.auth.to.UserInfoTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 在执行目标方法之前，判断用户的登录状态，并封装传递给controller目标请求
 */
public class CartInterceptor implements HandlerInterceptor {

    //ThreadLocal 同一个线程共享数据
    public static ThreadLocal<UserInfoTO> threadLocal = new ThreadLocal<>();
    //目标方法执行前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserInfoTO userInfoTO = new UserInfoTO();
        HttpSession session = request.getSession();
        MemberRespVO member = (MemberRespVO) session.getAttribute(AuthServerConstant.LOGIN_USER);
        if (member != null) {
            //用户已登录
            userInfoTO.setUserId(member.getId());
        }
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                //user-key
                String name = cookie.getName();
                if (name.equals(CartConstant.TEMP_USER_COOKIE_NAME)) {
                    userInfoTO.setUserKey(cookie.getValue());
                    userInfoTO.setTempUser(true);
                }
            }
        }

        //如果没有临时用户则分配一个
        if (StringUtils.isEmpty(userInfoTO.getUserKey())) {
            userInfoTO.setUserKey(UUID.randomUUID().toString());
        }
        threadLocal.set(userInfoTO);
        //非临时用户再更新cookie
        if (!userInfoTO.isTempUser()) {
            Cookie cookie = new Cookie(CartConstant.TEMP_USER_COOKIE_NAME, userInfoTO.getUserKey());
            //设置作用域
            cookie.setDomain("gulimall.com");
            //设置有效期
            cookie.setMaxAge(CartConstant.TEMP_USER_COOKIE_TIMEOUT);
            response.addCookie(cookie);
        }
        return true;
    }

    //todo 这里无法将cookie放入response，移动代码到上面，原因未知
    //目标方法执行后
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        UserInfoTO userInfoTO = threadLocal.get();
//        //非临时用户再更新cookie
//        if (!userInfoTO.isTempUser()) {
//            Cookie cookie = new Cookie(CartConstant.TEMP_USER_COOKIE_NAME, userInfoTO.getUserKey());
//            //设置作用域
//            cookie.setDomain("gulimall.com");
//            //设置有效期
//            cookie.setMaxAge(CartConstant.TEMP_USER_COOKIE_TIMEOUT);
//            response.addCookie(cookie);
//        }
//
//
//    }
}
